home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / files / t_sys5 / unixcpio.gz / unixnet.cpio / ax25user.c < prev    next >
C/C++ Source or Header  |  1994-07-11  |  3KB  |  158 lines

  1. /* User subroutines for AX.25 */
  2. #include "global.h"
  3. #include "mbuf.h"
  4. #include "timer.h"
  5. #include "iface.h"
  6. #include "ax25.h"
  7. #include "lapb.h"
  8. #include <ctype.h>
  9.  
  10. /* Open an AX.25 connection */
  11. struct ax25_cb *
  12. open_ax25(addr,window,r_upcall,t_upcall,s_upcall,iface,user)
  13. struct ax25 *addr;        /* Addresses */
  14. int16 window;            /* Window size in bytes */
  15. void (*r_upcall)();        /* Receiver upcall handler */
  16. void (*t_upcall)();        /* Transmitter upcall handler */
  17. void (*s_upcall)();        /* State-change upcall handler */
  18. struct interface *iface;    /* Hardware interface structure */
  19. char *user;            /* User linkage area */
  20. {
  21.     struct ax25_cb *axp,*cr_ax25();
  22.     void lapbstate();
  23.  
  24.     if((axp = cr_ax25(&addr->dest)) == NULLAX25)
  25.         return NULLAX25;
  26.     ASSIGN(axp->addr,*addr);
  27.     if(addr->ndigis != 0){
  28.         axp->t1.start *= (addr->ndigis + 1);
  29.         axp->t2.start *= (addr->ndigis + 1);
  30.         axp->t3.start *= (addr->ndigis + 1);
  31.     }
  32.     axp->window = window;
  33.     axp->r_upcall = r_upcall;
  34.     axp->t_upcall = t_upcall;
  35.     axp->s_upcall = s_upcall;
  36.     axp->interface = iface;
  37.     axp->user = user;
  38.  
  39.     switch(axp->state){
  40.     case DISCONNECTED:
  41.         /* Don't send anything if the connection already exists */
  42.         est_link(axp);
  43.         lapbstate(axp,SETUP);
  44.         break;
  45.     case SETUP:
  46.         free_q(&axp->txq);
  47.         break;
  48.     case DISCPENDING:    /* Ignore */
  49.     case FRAMEREJECT:
  50.         break;
  51.     case RECOVERY:
  52.     case CONNECTED:
  53.         free_q(&axp->txq);
  54.         est_link(axp);
  55.         lapbstate(axp,SETUP);
  56.         break;
  57.     }
  58.     return axp;
  59. }
  60.  
  61. /* Send data on an AX.25 connection. Caller must provide PID */
  62. int
  63. send_ax25(axp,bp)
  64. struct ax25_cb *axp;
  65. struct mbuf *bp;
  66. {
  67.     if(axp == NULLAX25 || bp == NULLBUF)
  68.         return -1;
  69.     enqueue(&axp->txq,bp);
  70.     return lapb_output(axp);
  71. }
  72.  
  73. /* Receive incoming data on an AX.25 connection */
  74. /*ARGSUSED*/
  75. struct mbuf *
  76. recv_ax25(axp,cnt)
  77. register struct ax25_cb *axp;
  78. int16 cnt;
  79. {
  80.     struct mbuf *bp;
  81.  
  82.     if(axp->rxq == NULLBUF)
  83.         return NULLBUF;
  84.  
  85.     bp = axp->rxq;
  86.     axp->rxq = NULLBUF;
  87.  
  88.     /* If this has un-busied us, send a RR to reopen the window */
  89.     if(len_mbuf(bp) >= axp->window)
  90.         sendctl(axp,RESPONSE,RR);
  91.     return bp;
  92. }
  93.  
  94. /* Close an AX.25 connection */
  95. disc_ax25(axp)
  96. struct ax25_cb *axp;
  97. {
  98.     void lapbstate();
  99.  
  100.     switch(axp->state){
  101.     case DISCONNECTED:
  102.         break;        /* Ignored */
  103.     case DISCPENDING:
  104.         lapbstate(axp,DISCONNECTED);
  105.         del_ax25(axp);
  106.         break;
  107.     case SETUP:
  108.     case CONNECTED:
  109.     case RECOVERY:
  110.     case FRAMEREJECT:
  111.         free_q(&axp->txq);
  112.         axp->retries = 0;
  113.         sendctl(axp,COMMAND,DISC|PF);
  114.         stop_timer(&axp->t3);
  115.         start_timer(&axp->t1);
  116.         lapbstate(axp,DISCPENDING);
  117.         break;
  118.     }
  119. }
  120.  
  121. /* Verify that axp points to a valid ax25 control block */
  122. ax25val(axp)
  123. struct ax25_cb *axp;
  124. {
  125.     register struct ax25_cb *axp1;
  126.     register int i;
  127.  
  128.     if(axp == NULLAX25)
  129.         return 0;    /* Null pointer can't be valid */
  130.     for(i=0; i < NHASH; i++)
  131.         for(axp1 = ax25_cb[i];axp1 != NULLAX25; axp1 = axp1->next)
  132.             if(axp1 == axp)
  133.                 return 1;
  134.     return 0;
  135. }
  136.  
  137. /* Force a retransmission */
  138. kick_ax25(axp)
  139. struct ax25_cb *axp;
  140. {
  141.     void recover();
  142.  
  143.     if(!ax25val(axp))
  144.         return -1;
  145.     recover((int *)axp);
  146.     return 0;
  147. }
  148.  
  149. /* Abruptly terminate an AX.25 connection */
  150. reset_ax25(axp)
  151. struct ax25_cb *axp;
  152. {
  153.     void lapbstate();
  154.  
  155.     lapbstate(axp,DISCONNECTED);
  156.     del_ax25(axp);
  157. }
  158.